
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.PagesCategories;

public class GenericPagesCatDAO extends AutoKeys implements org.jetnuke.dao.PagesCatDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesCatModel#retrieveByPK(int)
	 */
	public PagesCategories retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageCatModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		PagesCategories s = new PagesCategories();
		
		if (rs.next()) {
			fillPageCatFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillPageCatFromResultSet(PagesCategories s, ResultSet rs)throws Exception {
		s.setCID(rs.getInt("CID"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setTITLE(rs.getString("TITLE"));
		
	}

	
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			PagesCategories u = new PagesCategories();
			fillPageCatFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}
	public List selectAll() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageCatModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesCatModel#addNew(org.jetnuke.entities.PagesCategories)
	 */
	public int addNew(PagesCategories cat) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("PageCatModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PagesCatModel.lastGeneratedPagesCatID"));
		initNewPagesCat(cat, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
	}

	/**
	 * @param cat
	 * @param p
	 */
	private void initNewPagesCat(PagesCategories cat, PreparedStatement p) throws Exception{
		p.setString(1,cat.getDESCRIPTION());
		p.setString(2,cat.getTITLE());
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesCatModel#update(org.jetnuke.entities.PagesCategories)
	 */
	public void update(PagesCategories cat) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("PageCatModel.update");
		p.setString(1,cat.getDESCRIPTION());
		p.setString(2,cat.getTITLE());
		p.setInt(3,cat.getCID());
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesCatModel#selectAllOrderCID()
	 */
	public List selectAllOrderCID() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageCatModel.selectAllOrderCID"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesCatModel#selectAllDifferentCID(int)
	 */
	public List selectAllDifferentCID(int cid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageCatModel.selectAllDifferentCID"));
		p.setInt(1,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}


}
